package com.example.fw;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.PhoneData;

public class AllPrintPhonesHelper extends WebDriverHelperBase{
	
	public AllPrintPhonesHelper(AppManager manager) {
		super(manager);
	}	
		
	public List<PhoneData> getPhones() {
		List<PhoneData> phones = new ArrayList<PhoneData>(); 
		List<WebElement> contacts = getContacts();
		manager.navigateTo().allPrintPhonesPage();
		
		for (int i=0;i<getRows().size();i++) {
			for (int j=0;j<getColumns().size();j++){
				String contactData = getElements(By.xpath("//tr["+(i+1)+"]/td[@valign = 'top']["+(j+1)+"]")).get(0).getText();
				PhoneData phone = new PhoneData()
					.withData(contactData);
				
				phones.add(phone);
				
				if (phones.size() == contacts.size())		
					break;
			}			
		}		
		return phones;
	}

	public List<WebElement> getContacts() {
		manager.navigateTo().allPrintPhonesPage();
		return getElements(By.xpath("//td[@valign = 'top']"));
	}
	
	public List<WebElement> getRows() {
		return getElements(By.tagName("tr"));
	}
	
	public List<WebElement> getColumns() {
		return getElements(By.xpath("//tr[1]/td"));
	}
	
	
}
